package com.wannengbang.cloudleader.mine;

import android.os.Bundle;
import android.text.Editable;
import android.text.TextUtils;
import android.text.TextWatcher;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;

import com.google.gson.Gson;
import com.wannengbang.cloudleader.R;
import com.wannengbang.cloudleader.base.BaseActivity;
import com.wannengbang.cloudleader.base.DataCallBack;
import com.wannengbang.cloudleader.bean.BaseResponseBean;
import com.wannengbang.cloudleader.bean.UserInfoBean;
import com.wannengbang.cloudleader.login.model.ILoginModel;
import com.wannengbang.cloudleader.login.model.LoginModelImpl;
import com.wannengbang.cloudleader.utils.SPManager;
import com.wannengbang.cloudleader.utils.ToastUtil;
import com.wannengbang.cloudleader.widget.CountDownTimerButton;
import com.wannengbang.cloudleader.widget.ViewLoading;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

public class UpdateMobileActivity extends BaseActivity {

    @BindView(R.id.tv_back)
    TextView tvBack;
    @BindView(R.id.edit_mobile)
    EditText editMobile;
    @BindView(R.id.edit_code)
    EditText editCode;
    @BindView(R.id.tv_send_code)
    CountDownTimerButton tvSendCode;
    @BindView(R.id.edit_pwd)
    EditText editPwd;
    @BindView(R.id.tv_commit)
    TextView tvCommit;
    private ILoginModel loginModel;
    private UserInfoBean userInfoBean;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_update_mobile);
        ButterKnife.bind(this);
        initView();
    }

    public void initView() {
        userInfoBean = new Gson().fromJson(SPManager.getInstance().getUserInfoJson(), UserInfoBean.class);
        loginModel = new LoginModelImpl();
        editMobile.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {
                if (s.length() == 11) {
                    tvSendCode.setEnabled(true);
                } else {
                    tvSendCode.setEnabled(false);
                }
            }

            @Override
            public void afterTextChanged(Editable s) {

            }
        });
    }

    public void sendCode() {
        String mobile = editMobile.getText().toString();
        if (TextUtils.isEmpty(mobile)) {
            ToastUtil.showShort("请输入手机号");
            return;
        }
        loginModel.requestSendVerifyCode(mobile, "3", new DataCallBack<BaseResponseBean>() {
            @Override
            public void onSuccessful(BaseResponseBean bean) {
                ToastUtil.showShort(bean.getMsg());
            }

            @Override
            public void onFailed(String msg, String code) {

            }
        });
    }

    public void commit() {
        String mobile = editMobile.getText().toString();
        if (TextUtils.isEmpty(mobile)) {
            ToastUtil.showShort("请输入手机号");
            return;
        }
        String code = editCode.getText().toString();
        if (TextUtils.isEmpty(code)) {
            ToastUtil.showShort("请输入验证码");
            return;
        }
        String password = editPwd.getText().toString();
        if (TextUtils.isEmpty(password)) {
            ToastUtil.showShort("请输入登录密码");
            return;
        }
        ViewLoading.showProgress(mActivity, "加载中......");
        loginModel.requestEditMobile(mobile, code, password, new DataCallBack<BaseResponseBean>() {
            @Override
            public void onSuccessful(BaseResponseBean bean) {
                if (userInfoBean != null && userInfoBean.getData() != null) {
                    userInfoBean.getData().setMobile(mobile);
                    SPManager.getInstance().saveUserInfoJson(new Gson().toJson(userInfoBean));
                }
                ToastUtil.showShort(bean.getMsg());
                finish();
            }

            @Override
            public void onFailed(String msg, String code) {

            }
        });
    }

    @OnClick({R.id.tv_back, R.id.tv_send_code, R.id.tv_commit})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.tv_back:
                finish();
                break;
            case R.id.tv_send_code:
                sendCode();
                break;
            case R.id.tv_commit:
                commit();
                break;
        }
    }

}
